package com.huli.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.nio.charset.Charset;

@Configuration
public class RedisConfiguration {

    @Autowired
    private RedisConnectionFactory connectionFactory;

    @Bean("redisTemplateObject")
    public RedisTemplate<String,Object> createRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Object> rt=new RedisTemplate<>();
        rt.setConnectionFactory(connectionFactory);

        StringRedisSerializer srs=new StringRedisSerializer(Charset.forName("utf-8"));
        rt.setKeySerializer(srs);
        rt.setHashKeySerializer(srs);

        Jackson2JsonRedisSerializer<Object> jrs=new Jackson2JsonRedisSerializer<>(Object.class);
        rt.setValueSerializer(jrs);
        rt.setHashKeySerializer(jrs);

        return rt;
    }

}
